/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;

import com.gandulf.guilib.util.Debug;
import com.munchlevel.MunchLevelApplication;
import com.munchlevel.MyPreferenceActivity;
import com.munchlevel.R;
import com.munchlevel.preference.ListPreferenceMultiSelect;
import com.munchlevel.util.Util;

/**
 * @author Ganymede
 * 
 */
public class MunchkinConfiguration {

	public enum Edition {
		Munchkin("Munchkin"), Munchkin2("Munchkin 2"), Munchkin3("Munchkin 3"), Munchkin5("Munchkin 5"), Munchkin8(
				"Munchkin 8"), StarMunchkin("Star Munchkin"), MunchkinFu("Munchkin Fu"), SuperMunchkin("Super Munchkin"), MunchkinBites(
				"Munchkin Bites!"), MunchkinImpossible("Munchkin Impossible"), GoodBadMunchkin(
				"The Good, The Bad, and the Munchkin"), MunchkinCthuluh("Munchkin Cthulhu"), MunchkinBooty(
				"Munchkin Booty"), MunchkinZombies("Munchkin Zombies"), MunchkinZombies2("Munchkin Zombies 2"), MunchkinAxeCop(
				"Munchkin Axe Cop");

		private String title;

		private Edition(String t) {
			this.title = t;
		}

		public String getTitle() {
			return title;
		}

	}

	private Map<Edition, List<String>> classesMap;
	private Map<Edition, List<String>> racesMap;

	private List<String> classes;
	private List<String> races;

	private List<Edition> selectedEditions;

	private Context context;

	/**
	 * 
	 */
	public MunchkinConfiguration(Context context) {
		this.context = context;

		this.selectedEditions = new LinkedList<MunchkinConfiguration.Edition>();

		this.classesMap = new HashMap<MunchkinConfiguration.Edition, List<String>>();
		this.racesMap = new HashMap<MunchkinConfiguration.Edition, List<String>>();
		this.classes = new LinkedList<String>();
		this.races = new LinkedList<String>();

		Resources res = this.context.getResources();
		for (Edition e : Edition.values()) {

			Integer arrayId = Util.getArrayId("clazz_" + e.name());
			if (arrayId != null && arrayId >= 0) {
				String[] cl = res.getStringArray(arrayId);
				classesMap.put(e, Arrays.asList(cl));
			}

			arrayId = Util.getArrayId("race_" + e.name());
			if (arrayId != null && arrayId >= 0) {
				String[] cl = res.getStringArray(arrayId);
				racesMap.put(e, Arrays.asList(cl));
			}

		}

	}

	public List<Edition> getSelectedEditions() {
		return selectedEditions;
	}

	public void setSelectedEditions(Edition... editions) {
		setSelectedEditions(Arrays.asList(editions));
	}

	public void setSelectedEditions(String... editions) {

		List<Edition> ed = new ArrayList<Edition>(editions.length);

		for (int i = 0; i < editions.length; i++) {
			// there have been problems with old default values #ALL# etc...
			try {
				ed.add(Edition.valueOf(editions[i]));
			} catch (Exception e) {
				Debug.warning("Illegal enumeration value for editions skipping it:" + editions[i]);
			}
		}

		// failsafe to have at least the basic eidtion
		if (ed.isEmpty()) {
			ed.add(Edition.Munchkin);
		}

		setSelectedEditions(ed);
	}

	public static String getNoClass() {
		return MunchLevelApplication.getInstance().getResources().getString(R.string.clazz_none);
	}

	public static String getNoRace() {
		return MunchLevelApplication.getInstance().getResources().getString(R.string.race_none);
	}

	public void reload(SharedPreferences preferences) {
		String editionRow = preferences.getString(MyPreferenceActivity.KEY_EDITIONS, null);
		String[] editions = ListPreferenceMultiSelect.parseStoredValue(editionRow);
		if (editions != null)
			setSelectedEditions(editions);
		else
			setSelectedEditions(Edition.Munchkin);
	}

	public void setSelectedEditions(List<Edition> editions) {

		classes.clear();
		races.clear();

		classes.add(getNoClass());
		races.add(getNoRace());

		this.selectedEditions = editions;

		for (Edition e : editions) {
			if (classesMap.containsKey(e))
				classes.addAll(classesMap.get(e));

			if (racesMap.containsKey(e))
				races.addAll(racesMap.get(e));
		}

	}

	public List<String> getClasses() {
		return classes;
	}

	public List<String> getRaces() {
		return races;
	}
}
